package com.mbyte.easy.rest.usecase;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.admin.entity.Usecase;
import com.mbyte.easy.admin.service.IUsecaseService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@RestController
@RequestMapping("rest/usecase")
public class RestUsecaseController extends BaseController  {

    @Autowired
    private IUsecaseService usecaseService;

    /**
    * 查询列表
    *
    * @param pageNo
    * @param pageSize
    * @param usecase
    * @return
    */
    @RequestMapping
    public AjaxResult index(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String caseCreatetimeSpace, String caseUpdatetimeSpace, Usecase usecase) {
        Page<Usecase> page = new Page<Usecase>(pageNo, pageSize);
        QueryWrapper<Usecase> queryWrapper = new QueryWrapper<Usecase>();

        if(usecase.getEquId() != null  && !"".equals(usecase.getEquId() + "")) {
            queryWrapper = queryWrapper.like("equ_id",usecase.getEquId());
         }


        if(usecase.getEquName() != null  && !"".equals(usecase.getEquName() + "")) {
            queryWrapper = queryWrapper.like("equ_name",usecase.getEquName());
         }


        if(usecase.getEquType() != null  && !"".equals(usecase.getEquType() + "")) {
            queryWrapper = queryWrapper.like("equ_type",usecase.getEquType());
         }


        if(usecase.getEquUser() != null  && !"".equals(usecase.getEquUser() + "")) {
            queryWrapper = queryWrapper.like("equ_user",usecase.getEquUser());
         }


        if(usecase.getEquStatus() != null  && !"".equals(usecase.getEquStatus() + "")) {
            queryWrapper = queryWrapper.like("equ_status",usecase.getEquStatus());
         }


        if(usecase.getEquStaff() != null  && !"".equals(usecase.getEquStaff() + "")) {
            queryWrapper = queryWrapper.like("equ_staff",usecase.getEquStaff());
         }


        if(usecase.getCaseCreatetime() != null  && !"".equals(usecase.getCaseCreatetime() + "")) {
            queryWrapper = queryWrapper.like("case_createtime",usecase.getCaseCreatetime());
         }


        if(usecase.getCaseSite() != null  && !"".equals(usecase.getCaseSite() + "")) {
            queryWrapper = queryWrapper.like("case_site",usecase.getCaseSite());
         }


        if(usecase.getCaseUpdatetime() != null  && !"".equals(usecase.getCaseUpdatetime() + "")) {
            queryWrapper = queryWrapper.like("case_updatetime",usecase.getCaseUpdatetime());
         }

        IPage<Usecase> pageInfo = usecaseService.page(page, queryWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("searchInfo",  usecase);
        map.put("pageInfo", new PageInfo(pageInfo));

        return this.success(map);
    }


    /**
    * 添加
    * @param usecase
    * @return
    */
    @PostMapping("add")
    public AjaxResult add(Usecase usecase){
        return toAjax(usecaseService.save(usecase));
    }

    /**
    * 添加
    * @param usecase
    * @return
    */
    @PostMapping("edit")
    public AjaxResult edit(Usecase usecase){
        return toAjax(usecaseService.updateById(usecase));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(usecaseService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(usecaseService.removeByIds(ids));
    }

}

